export const downloadFile = async (option = {}) => {
    let _url = option.url.replace(/^https?:\/\/(.*?)(:\d+)?\//, "/");
    _url = _url.split("/");
    console.log(_url);
    wx.downloadFile({
        url: option.url || "",
        header: option.header || {},
        timeout: option.timeout || 30000,
        filePath: option.filePath || wx.env.USER_DATA_PATH + "/" + _url[_url.length - 1],
        success(res) {
            console.log(res);
            const fileName = _url[_url.length - 1];
            saveFile(res, fileName.indexOf(".mp4") !== -1 ? "video" : "", option);
        },
        fail(res) {
            console.log("error", res);
            let msg = "";
            if (res.errMsg.indexOf("fail auth deny") !== -1) {
                msg = "用户拒绝授权，请重新进入小程序授权";
            } else {
                msg = "下载文件失败" + res.errMsg;
            }
            wx.askShowToastEx({
                icon: "none",
                title: msg,
                cancel() {
                    option.fail && option.fail(res);
                },
            });
        }
    });
};

const saveFile = (res, type, option) => {
    if (type === "video") {
        wx.saveVideoToPhotosAlbum({
            filePath: res.filePath,
            success(res) {
                console.log(res);
                wx.nextTick(() => {
                    wx.askShowToastEx({
                        icon: "none",
                        title: "文件已下载",
                    });
                    option.success && option.success(res);
                });
            },
            fail(res) {
                console.log("saveError", res);
                wx.nextTick(() => {
                    let msg = "";
                    if (res.errMsg.indexOf("fail auth deny") !== -1) {
                        msg = "用户拒绝授权，请重新进入小程序授权";
                    } else {
                        msg = "保存文件失败" + res.errMsg;
                    }
                    wx.askShowToastEx({
                        icon: "none",
                        title: msg,
                        cancel() {
                            option.fail && option.fail(res);
                        },
                    });
                });
            },
            complete(res) {
                option.complete && option.complete(res);
            },
        });
    }else{
        wx.saveFile({
            tempFilePath: res.filePath,
            success(res) {
                console.log(res);
                wx.nextTick(() => {
                    wx.askShowToastEx({
                        icon: "none",
                        title: "文件已下载至" + res.savedFilePath,
                    });
                    option.success && option.success(res);
                });
            },
            fail(res) {
                console.log("saveError", res);
                saveDisk(res, option);
            },
            complete(res) {
                // option.complete && option.complete(res);
            },
        });
    }
};

const saveDisk = (res, option)=>{
    try{
        wx.saveFileToDisk({
            filePath: res.filePath,
            success(res) {
                wx.nextTick(() => {
                    wx.askShowToastEx({
                        icon: "none",
                        title: "文件已下载至" + res.savedFilePath,
                    });
                    option.success && option.success(res);
                });
            },
            fail(res) {
                console.log("saveError", res);
                wx.nextTick(() => {
                    let msg = "";
                    if (res.errMsg.indexOf("fail auth deny") !== -1) {
                        msg = "用户拒绝授权，请重新进入小程序授权";
                    } else {
                        msg = "保存文件失败" + res.errMsg;
                    }
                    wx.askShowToastEx({
                        icon: "none",
                        title: msg,
                        cancel() {
                            option.fail && option.fail(res);
                        },
                    });
                });
                saveDisk();
            },
            complete(res) {
                option.complete && option.complete(res);
            },
        });
    }catch(e){
        
        wx.showToast({
          title: e,
          icon:"none"
        })
    }

};